package com.comleader.operator.service;

import static com.comleader.operator.pojo.entity.table.PersonalUserTableDef.PERSONAL_USER;
import static com.mybatisflex.core.query.QueryMethods.distinct;
import static com.mybatisflex.core.query.QueryMethods.select;

import java.util.List;

import org.springframework.stereotype.Service;

import com.comleader.operator.mapper.PersonalUserMapper;
import com.comleader.operator.pojo.entity.PersonalUserEntity;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;

/**
 * 个人用户表 服务层实现。
 *
 * @author 连新景
 * @since 1.0
 */
@Service
public class PersonalUserService extends CustomService<PersonalUserMapper, PersonalUserEntity> {

    /**
     * @Description 查询一证多名的数据
     * @Auhtor 连新景
     * @Date 2024/08/12 16:19
     * @param customerType 客户类型
     * @param userBusinessType 用户业务类型
     * @param idNumber 身份证号列
     * @param name 姓名列
     * @return java.util.List<com.comleader.operator.pojo.entity.PersonalUserEntity>
     */
    public List<PersonalUserEntity> listByTypeAndName(String customerType, String userBusinessType, QueryColumn idNumber, QueryColumn name) {
        QueryWrapper queryWrapper = QueryWrapper.create()
            .where(idNumber.in(select(idNumber).from(PERSONAL_USER)
                .where(PERSONAL_USER.CUSTOMER_TYPE.eq(customerType).and(PERSONAL_USER.USER_BUSINESS_TYPE.eq(userBusinessType))).groupBy(idNumber)
                .having(QueryMethods.count(distinct(name)).gt(1))));
        return list(queryWrapper);
    }

    /**
     * @Description 查询一证五号的数据（同一机主证件，办理超过5张电话卡的数据）
     * @Auhtor 连新景
     * @Date 2024/08/12 15:53
     * @return java.util.List<com.comleader.operator.pojo.entity.PersonalUserEntity>
     */
    public List<PersonalUserEntity> listOneCardFiveNumbers() {
        QueryWrapper queryWrapper = QueryWrapper.create().where(PERSONAL_USER.OWNER_ID_NUMBER.in(select(PERSONAL_USER.OWNER_ID_NUMBER).from(PERSONAL_USER)
            .groupBy(PERSONAL_USER.OWNER_ID_NUMBER).having(QueryMethods.count(distinct(PERSONAL_USER.USER_NUMBER)).gt(5))));
        return list(queryWrapper);
    }

}